package my2Cents.tools;

import java.awt.Graphics2D;
import java.awt.event.MouseEvent;

import my2Cents.Sandbox;
import my2Cents.Util;
import net.phys2d.math.Vector2f;
import net.phys2d.raw.Body;
import net.phys2d.raw.shapes.PBox;
import net.phys2d.raw.shapes.PCircle;

public class BoxTool extends AbstractBodyMaker {
	private Vector2f dimension = new Vector2f();
	private int stage = 0;
	private Vector2f startPos;
	private double initAngle;
	private boolean shouldDraw = false;
	
	public BoxTool() {
		thinkDelay = false;
		this.thinkLevel = this.THINKLEVEL_MAX;		
		setName("Box");
		setDescription(
			"Click and drag to create a box.\n" +
			"When box is created depress mouse button\n" +
			"and move mouse to rotate.\n"+
			"Click once more to finish."
		);
	}
	
	public void startTool(MouseEvent arg0) {
		shouldDraw = false;
		if(stage == 0) {
			super.startTool(arg0);
			startPos = new Vector2f();
			startPos.x = arg0.getX();
			startPos.y = arg0.getY();
		}
		shapeAngle = 0.0f;
		Vector2f vec = new Vector2f(startPos);			
		Vector2f vec2 = new Vector2f(vec);
		vec2.x += dimension.x/2;
		vec2.y -= dimension.y/2;
		initAngle = Math.toDegrees(Util.angleVec(vec2, vec));
	}
	
	public void toolThink(MouseEvent arg0) {
		super.toolThink(arg0);
		if(stage == 0) {
			dimension.x = (shapePos.x - arg0.getX());
			dimension.y = (shapePos.y - arg0.getY());

			float modx = Math.max(dimension.x,0);
			float mody = Math.max(dimension.y,0);
			dimension.x = Math.min(dimension.x,0);
			dimension.y = Math.min(dimension.y,0);
			shapePos.x -= modx;
			shapePos.y -= mody;
			shapeAngle = 0;
			//shapeAngle = (float) angleVec(shapePos, vec);
			float dx = dimension.x;
			float dy = dimension.y;
			//shapePos.x = startPos.x + dimension.x;
			if(dx < 0) dx*=-1;
			if(dy < 0) dy*=-1;
			shape = new PBox(dx, dy);
			ghostOffset.x =	-dimension.x/2;
			ghostOffset.y = -dimension.y/2;
			adjustGhostOffset();
			//ghostOffset.x += dimension.x;
			//ghostOffset.y += dimension.y;
			shouldDraw = true;
		} else if(stage == 1) {
			Vector2f vec = new Vector2f(arg0.getX(), arg0.getY());			
			Vector2f vec2 = new Vector2f(shapePos);
			vec2.x -= dimension.x/2;
			vec2.y -= dimension.y/2;
			double temp = Math.toDegrees(Util.angleVec(vec2, vec));
			double temp2 = temp + initAngle;
			shapeAngle = (float) (temp2/57.3);
			shouldDraw = true;
		}
	}
	
	public void endTool(MouseEvent arg0) {
		if(stage == 0) {
			stage++;
			startTool(null);
			if(!(dimension.length() > 10)) {
				shouldDraw = false;
				stage = 1;
				shape = null;
				shapePos = new Vector2f();
				shapeAngle = 0;
				dimension = new Vector2f();
				endTool(arg0);
			}
		} else if(stage == 1) {
			if(dimension.length() > 10) {
				float size = (float)Math.sqrt(dimension.x*dimension.x + dimension.y*dimension.y);
				Body b = createBody(size);
				//System.out.println(shape.getSurfaceFactor()/1000);
				b.setPosition(shapePos.x-dimension.x/2, shapePos.y-dimension.y/2);
				b.setRotation(shapeAngle);
				sandbox.postBodyCreated(b);
			}
			sandbox.onToolEnd();
			super.endTool(arg0);
			stage = 0;
		}
	}
	
	public void drawGhost(Graphics2D g) {
		if(shouldDraw) {
			super.drawGhost(g);
		}
	}
}
